﻿using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using log4net;

namespace com.szmxsoft.yeming
{
	// Token: 0x02000018 RID: 24
	public class XmlUtil
	{
		// Token: 0x0600003E RID: 62 RVA: 0x000028AC File Offset: 0x00000AAC
		public static XmlDocument init()
		{
			if (XmlUtil.configXmlPath == null)
			{
				XmlUtil.configXmlPath = AppDomain.CurrentDomain.BaseDirectory + "config\\\\system.xml";
				if (!File.Exists(XmlUtil.configXmlPath))
				{
					Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "config");
					XmlUtil.doc = new XmlDocument();
					XmlUtil.initXml(XmlUtil.doc);
					XmlUtil.doc.Save(XmlUtil.configXmlPath);
				}
			}
			if (XmlUtil.doc == null)
			{
				XmlUtil.doc = new XmlDocument();
				XmlUtil.doc.Load(XmlUtil.configXmlPath);
			}
            return XmlUtil.doc;
		}

		// Token: 0x0600003F RID: 63 RVA: 0x00002944 File Offset: 0x00000B44
		private static void initXml(XmlDocument doc)
		{
			XmlDeclaration newChild = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
			doc.InsertBefore(newChild, doc.DocumentElement);
			XmlNode xmlNode = doc.CreateElement("system");
			XmlNode xmlNode2 = doc.CreateElement("userLoginName");
			xmlNode.AppendChild(xmlNode2);
			xmlNode2 = doc.CreateElement("ipaddress");
			xmlNode.AppendChild(xmlNode2);
			xmlNode2 = doc.CreateElement("dbuser");
			xmlNode.AppendChild(xmlNode2);
			xmlNode2 = doc.CreateElement("dbpassword");
			xmlNode.AppendChild(xmlNode2);
			xmlNode2 = doc.CreateElement("dbname");
			xmlNode.AppendChild(xmlNode2);
			xmlNode2 = doc.CreateElement(Constant.FRUSH_CARD);
			xmlNode2.InnerText = Encrypt.Encrypt3Des("COM0");
			xmlNode.AppendChild(xmlNode2);
			xmlNode2 = doc.CreateElement(Constant.REGISTER_CODE);
			xmlNode.AppendChild(xmlNode2);
			xmlNode2 = doc.CreateElement(Constant.FRUSH_CARD);
			xmlNode.AppendChild(xmlNode2);
			xmlNode2 = doc.CreateElement(Constant.REGISTER_TIME);
			xmlNode.AppendChild(xmlNode2);
			xmlNode2 = doc.CreateElement(Constant.SCAN_TYPE);
			xmlNode2.InnerText = Encrypt.Encrypt3Des(Constant.SCAN_TYPE_531);
			xmlNode.AppendChild(xmlNode2);
			doc.AppendChild(xmlNode);
		}

		// Token: 0x06000040 RID: 64 RVA: 0x00002A74 File Offset: 0x00000C74
		public static string getXmlInfo(string node)
		{
			try
			{
				if (XmlUtil.doc == null)
				{
					XmlUtil.init();
				}
				string innerText = XmlUtil.doc.SelectSingleNode("system").SelectSingleNode(node).InnerText;
				return Encrypt.Decrypt3Des(innerText);
			}
			catch (Exception ex)
			{
				XmlUtil.LOGGER.Error("从XML文件中获取一条记录发生错误:" + ex.Message);
			}
			return null;
		}

		// Token: 0x06000041 RID: 65 RVA: 0x00002AE4 File Offset: 0x00000CE4
		public static string getXmlInfoCommon(string node)
		{
			try
			{
				if (XmlUtil.doc == null)
				{
					XmlUtil.init();
				}
				return XmlUtil.doc.SelectSingleNode("system").SelectSingleNode(node).InnerText;
			}
			catch (Exception ex)
			{
				XmlUtil.LOGGER.Error("从XML文件中获取一条记录发生错误:" + ex.Message);
			}
			return null;
		}

		// Token: 0x06000042 RID: 66 RVA: 0x00002B4C File Offset: 0x00000D4C
		public static void saveXmlByNode(string node, string value)
		{
			try
			{
				if (XmlUtil.doc == null)
				{
					XmlUtil.init();
				}
				string text = XmlUtil.doc.SelectSingleNode("system").SelectSingleNode(node).InnerText;
				if (Utils.checkStr(text))
				{
					text = "";
				}
				else
				{
					text = Encrypt.Decrypt3Des(text);
				}
				bool flag = false;
				string[] array = text.Split(new char[]
				{
					'+'
				});
				for (int i = 0; i < array.Length; i++)
				{
					if (value.Equals(array[i]))
					{
						flag = true;
					}
				}
				if (!flag)
				{
					if (Utils.checkStr(text))
					{
						text = value;
					}
					else
					{
						text = text + "+" + value;
					}
					XmlUtil.doc.SelectSingleNode("system").SelectSingleNode(node).InnerText = Encrypt.Encrypt3Des(text);
					XmlUtil.doc.Save(XmlUtil.configXmlPath);
				}
			}
			catch (Exception ex)
			{
				XmlUtil.LOGGER.Error("从XML文件中获取一条记录发生错误:" + ex.Message);
			}
		}

		// Token: 0x06000043 RID: 67 RVA: 0x00002C4C File Offset: 0x00000E4C
		public static void saveSingleNode(string node, string value)
		{
			try
			{
				if (XmlUtil.doc == null)
				{
					XmlUtil.init();
				}
				XmlNode xmlNode = XmlUtil.doc.SelectSingleNode("system").SelectSingleNode(node);
				if (xmlNode != null)
				{
					XmlUtil.doc.SelectSingleNode("system").SelectSingleNode(node).InnerText = Encrypt.Encrypt3Des(value);
					XmlUtil.doc.Save(XmlUtil.configXmlPath);
				}
				else
				{
					xmlNode = XmlUtil.doc.CreateElement(node);
					xmlNode.InnerText = Encrypt.Encrypt3Des(value);
					XmlUtil.doc.SelectSingleNode("system").AppendChild(xmlNode);
					XmlUtil.doc.Save(XmlUtil.configXmlPath);
				}
			}
			catch (Exception ex)
			{
				XmlUtil.LOGGER.Error("保存一个信息到XML文件中发生错误:" + ex.Message);
			}
		}

		// Token: 0x06000044 RID: 68 RVA: 0x00002D1C File Offset: 0x00000F1C
		public static void saveXmlUser(string username, string password)
		{
			try
			{
				if (XmlUtil.doc == null)
				{
					XmlUtil.init();
				}
				string xmlInfo = XmlUtil.getXmlInfo("userLoginName");
				if (!Utils.checkStr(xmlInfo))
				{
					StringBuilder stringBuilder = new StringBuilder();
					string[] array = xmlInfo.Split(new char[]
					{
						'|'
					});
					bool flag = false;
					foreach (string text in array)
					{
						string[] array3 = text.Split(new char[]
						{
							':'
						});
						if (array3.Length == 2)
						{
							if (array3[0] == username)
							{
								stringBuilder.Append(username).Append(":").Append(password).Append("|");
								flag = true;
							}
							else
							{
								stringBuilder.Append(text).Append("|");
							}
						}
					}
					if (!flag)
					{
						stringBuilder.Append(username).Append(":").Append(password);
					}
					if (stringBuilder.ToString().EndsWith("|"))
					{
						XmlUtil.saveSingleNode("userLoginName", stringBuilder.ToString().Substring(0, stringBuilder.Length - 1));
					}
					else
					{
						XmlUtil.saveSingleNode("userLoginName", stringBuilder.ToString());
					}
				}
				else
				{
					StringBuilder stringBuilder2 = new StringBuilder();
					stringBuilder2.Append(username).Append(":").Append(password);
					XmlUtil.saveSingleNode("userLoginName", stringBuilder2.ToString());
				}
				XmlUtil.doc.Save(XmlUtil.configXmlPath);
			}
			catch (Exception ex)
			{
				XmlUtil.LOGGER.Error("保存用户登录的用户名与密码发生错误:" + ex.Message);
			}
		}

		// Token: 0x040000DC RID: 220
		private static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

		// Token: 0x040000DD RID: 221
		public static string configXmlPath = null;

		// Token: 0x040000DE RID: 222
		private static XmlDocument doc = null;
	}
}
